package cn.com.blueInfo.bpm.common.repository;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.entity.BaseExtend;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.enums.BaseStatus;
import cn.com.blueInfo.bpm.common.request.BaseQueryDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import cn.com.blueInfo.utils.error.BusinessException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;

public interface BaseRepository<T extends BaseEntity, DTO extends BaseQueryDTO> {

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    CurrentLoginUserInfo getCurrentLoginUserInfo();

    /**
     * 添加公共查询条件
     * @param queryWrapper 字段值
     * @param queryDTO 查询内容
     * @param columnMap 查询字段
     */
    default void addQueryWrapper(
            LambdaQueryWrapper<T> queryWrapper, DTO queryDTO,
            Map<String, SFunction<T, ?>> columnMap) {

        // 从实现类获取实际实体类型（如UserBase.class）
        Long tenantId = getCurrentLoginUserInfo().getTenantId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(tenantId), columnMap.get(BaseEntity.TENANT_ID), tenantId);

        Long applicationId = getCurrentLoginUserInfo().getApplicationId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(applicationId), columnMap.get(BaseEntity.APPLICATION_ID), applicationId);

        BaseDeleted deleted = queryDTO.getDeleted();
//        queryWrapper.eq(ObjectUtils.isEmpty(deleted), columnMap.get(BaseEntity.DELETED), BaseDeleted.NOT_DELETED);
        queryWrapper.eq(ObjectUtils.isNotEmpty(deleted), columnMap.get(BaseEntity.DELETED), deleted);

        BaseStatus status = queryDTO.getStatus();
//        queryWrapper.eq(ObjectUtils.isEmpty(status), columnMap.get(BaseEntity.STATUS), BaseStatus.ENABLED);
        queryWrapper.eq(ObjectUtils.isNotEmpty(status), columnMap.get(BaseEntity.STATUS), status);

        Long createUserId = queryDTO.getCreateUserId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(createUserId), columnMap.get(BaseEntity.CREATE_USER_ID), createUserId);

        LocalDateTime startCreateTime = queryDTO.getStartCreateTime();
        LocalDateTime endCreateTime = queryDTO.getEndCreateTime();
        queryWrapper.between(ObjectUtils.isNotEmpty(startCreateTime) && ObjectUtils.isNotEmpty(endCreateTime),
                columnMap.get(BaseEntity.CREATE_TIME), startCreateTime, endCreateTime);

        Long updateUserId = queryDTO.getUpdateUserId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(updateUserId), columnMap.get(BaseEntity.UPDATE_USER_ID), updateUserId);

        LocalDateTime startUpdateTime = queryDTO.getStartUpdateTime();
        LocalDateTime endUpdateTime = queryDTO.getEndUpdateTime();
        queryWrapper.between(ObjectUtils.isNotEmpty(startUpdateTime) && ObjectUtils.isNotEmpty(endUpdateTime),
                columnMap.get(BaseEntity.UPDATE_TIME), startUpdateTime, endUpdateTime);

        Long deleteUserId = queryDTO.getDeleteUserId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(deleteUserId), columnMap.get(BaseEntity.DELETE_USER_ID), deleteUserId);

        LocalDateTime startDeleteTime = queryDTO.getStartDeleteTime();
        LocalDateTime endDeleteTime = queryDTO.getEndDeleteTime();
        queryWrapper.between(ObjectUtils.isNotEmpty(startDeleteTime) && ObjectUtils.isNotEmpty(endDeleteTime),
                columnMap.get(BaseEntity.DELETE_TIME), startDeleteTime, endDeleteTime);

        Integer orderNumber = queryDTO.getOrderNumber();
        queryWrapper.eq(ObjectUtils.isNotEmpty(orderNumber), columnMap.get(BaseEntity.ORDER_NUMBER), orderNumber);

        String remark = queryDTO.getRemark();
        queryWrapper.eq(StringUtils.isNotBlank(remark), columnMap.get(BaseEntity.REMARK), remark);

        String extend1 = queryDTO.getExtend1();
        queryWrapper.eq(StringUtils.isNotBlank(extend1), columnMap.get(BaseExtend.EXTEND_1), extend1);

        String extend2 = queryDTO.getExtend2();
        queryWrapper.eq(StringUtils.isNotBlank(extend2), columnMap.get(BaseExtend.EXTEND_2), extend2);

        JSONObject extend3 = queryDTO.getExtend3();
        queryWrapper.eq(ObjectUtils.isNotEmpty(extend3), columnMap.get(BaseExtend.EXTEND_3), extend3);

    }

    /**
     * 验证更新ID是否存在
     * @param id 主键
     */
    default void validateUpdateId(Long id) {
        if (id == null || String.valueOf(id).length() < 10) {
            throw new BusinessException(500, "没有ID不能更新数据行！");
        }
    }

    /**
     * 返回数字类型的SQL执行结果
     * @param execute 执行结果
     * @return Integer
     */
    default Integer sqlExecuteResult(Boolean execute) {
        return execute ? 1 : 0;
    }

    default Integer sqlExecuteResult(Boolean execute, Integer size) {
        return execute ? size : 0;
    }

}
